<?php
namespace app\service\controller;
use think\Db;
use app\index\model\Ts ;
use app\index\model\Content;
class Timo extends \think\Controller{
    function getcontent(){
        
        $language=$this->request->get('language');
        $page=$this->request->get('page');
        $name=$this->request->get('name');
        $type=$this->request->get('type');
        $a_id=$this->request->get('a_id');
        
        $where="";
        
        if(isset($type)){
            $where.=" AND c.type='".$type."'";
        }
        
        if(isset($a_id)){
            $where.=" AND c.a_id='".$a_id."'";
            
           
        }
        
        if(isset($name)){
            $where.=" AND c.name = '".$name."'";
            
           
        }
        

        if(!isset($language)){
            $re['code']=400;
            $re['msg']='语言参数不存在';
            $re['data']='';
            echo json_encode($re);
            exit;
        }
        
       
        
        if(!isset($page)){
            $page=1;
        }
      
        
        
 
            
            
        
                $start=($page-1)*20;
                $sql="select c.a_id,c.name as zh_name,c.content,t.name as en_name from content c left join ts_info_en t on t.a_id=c.a_id where c.is_video=1 ".$where." limit ".$start.", 20 ";
         
            
            
            $result = Db::query($sql);
            foreach ($result as $k=>$v){
                
                
                $result[$k]['audio_name_zh_url']='';
                $result[$k]['audio_name_en_url']='';
                
                
                $s1="select * from file_list where a_id='".$v['a_id']."' and type=4 ";
                $res1=Db::query($s1);
 
                foreach ($res1 as $k1=>$v1){
                   
                        if($v1['language']=="zh"){
                            $result[$k]['audio_name_zh_url']="https://timo-video.oss-cn-shanghai.aliyuncs.com/".$v1['oss_key'];
                        }
                        
                        if($v1['language']=="eng"){
                            $result[$k]['audio_name_en_url']="https://timo-video.oss-cn-shanghai.aliyuncs.com/".$v1['oss_key'];
                        }
                    
                }
                
                
                $s="select * from file_list where a_id='".$v['a_id']."' and type>1 ";
                $res=Db::query($s);
                foreach ($res as $k1=>$v1){
                    if($v1['type']==2){
                        if($language=="zh"){
                            if($v1['language']=="zh"){
                                $result[$k]['audio_url']="https://timo-video.oss-cn-shanghai.aliyuncs.com/".$v1['oss_key'];
                            }
                        }
                        else{
                            if($v1['language']=="eng"){
                                $result[$k]['audio_url']="https://timo-video.oss-cn-shanghai.aliyuncs.com/".$v1['oss_key'];
                            }
                        }
                        
                    }
                    
                    if($v1['type']==3){
                        if($language=="zh"){
                            if($v1['language']=="zh"){
                                $result[$k]['video_url']="https://timo-video.oss-cn-shanghai.aliyuncs.com/".$v1['oss_key'];
                            }
                        }else{
                            if($v1['language']=="eng"){
                                $result[$k]['video_url']="https://timo-video.oss-cn-shanghai.aliyuncs.com/".$v1['oss_key'];
                            }
                        }
                        
                    }
                    
                    
                }
                if(isset($name)){
                    $s1="select * from content where name='".$name."'";
                    $r=Db::query($s1);
                    if(count($r)>0){
                        
                    }
                    else{
                        $insert_sql="insert into content set name='".$name."' ,is_video=0";
                        Db::execute($insert_sql);
                    }
                    
                }
            }
        
        

        $re['code']=0;
        $re['data']=$result;
        $re['msg']='sucess';
        echo json_encode($re);
        exit;
    }
    
    function savecontent($a_id){
        $name=$this->request->post('name');
        $content=$this->request->post('content');
        $type=$this->request->post('type');
        $type2=$this->request->post('type2');
        
        $en_name=$this->request->post('en_name');
        $en_content=$this->request->post('en_content');

        if(isset($a_id)&&$a_id>0){
            $ts =new Ts();
            $ts->save([
                'name'  => $en_name,
                'content' => $en_content
            ],['a_id' => $a_id]);

            $c=Content::get($a_id);
            $c->name=$name;
            $c->content=$content;
            $c->type=$type;
            $c->type2=$type2;
            $c->save();
            
           
            echo 'sucess';
            exit();
        }
        else{
            $c = new Content();
            $c->name=$name;
            $c->content=$content;
            $c->type=$type;
            $c->type2=$type2;
            $c->save();
            
            echo $c->a_id;
            exit();
        }
    }
    
    function changetype(){
        $type_id = $this->request->post('type_id');
        $where=array('p_id'=>$type_id);
        $cat_list=Db::name('content_type')->where($where)->select();
        echo json_encode($cat_list);
        exit;
    }
}